using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace TALoaders.Tests
{
    using NUnit.Framework;

    [TestFixture]
    class TestUtilities
    {
        [Test]
        [Category("Calculation")]
        public void testDataTableCSVSaveAndLoad()
        {
            DataTable dTable = new DataTable();
            dTable.Columns.Add("Date", typeof(DateTime));
            dTable.Columns.Add("Loader", typeof(Loader));
            dTable.Columns.Add("Ticker", typeof(string));
            dTable.Columns.Add("count", typeof(int));
            dTable.Columns.Add("price", typeof(double));

            // row 1
            DataRow row = dTable.NewRow();
            row["Date"] = new DateTime(2010, 1, 1);
            row["Loader"] = Loader.Bloomberg;
            row["Ticker"] = "RX1 Index";
            row["count"] = 1;
            row["price"] = 3.1415;
            dTable.Rows.Add(row);
            // row 2
            row = dTable.NewRow();
            row["Date"] = new DateTime(2010, 1, 1);
            row["Loader"] = Loader.Yahoo;
            row["Ticker"] = "^OMX";
            row["count"] = 2;
            row["price"] = 2.7;
            dTable.Rows.Add(row);

            // save:
            string tmpFileName = "tmpDataTable.csv";
            Utilities.saveDataTableToCSV(tmpFileName, dTable);
            FileAssert.AreEqual("..\\..\\Tests\\DataTable.csv", tmpFileName, "Save to CSV failed");

            dTable.Clear();
            Assert.AreEqual(0, dTable.Rows.Count, "DataTable should be empty now");

            Utilities.loadCSVToDataTable(tmpFileName, ref dTable);
            Assert.AreEqual(2, dTable.Rows.Count, "DataTable should be loaded now");
            Assert.AreEqual(Loader.Bloomberg, (Loader)Enum.ToObject(typeof(Loader),dTable.Rows[0]["Loader"]), "Compare Loaders");
            string tmp2FileName = "tmp2DataTable.csv";
            Utilities.saveDataTableToCSV(tmp2FileName, dTable);
            FileAssert.AreEqual(tmpFileName, tmp2FileName, "both tmp files should be same");

            File.Delete(tmpFileName);
            File.Delete(tmp2FileName);
        }
    }
}
